package dao.mysql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import model.HolidayCalendarDTO;
import model.LeaveTypeDTO;
import exception.DAOSystemException;
import dao.HolidayCalendarDAO;

public class HolidayCalendarDAOImpl implements HolidayCalendarDAO {
	MySqlImpl mysqlImpl = new MySqlImpl();
	@Override
	public HolidayCalendarDTO findHolidayCalendar(String id)
			throws DAOSystemException {
		// TODO Auto-generated method stub
		String selectSql = "SELECT * FROM holidaycalendar WHERE id='" + id.trim()
		+ "';";
HolidayCalendarDTO oneHolidayCalendar = new HolidayCalendarDTO();
try {
	ResultSet rs = mysqlImpl.get(selectSql);

	String leaveTypeId = null;
	while (rs.next()) {
		oneHolidayCalendar.setId(rs.getString("id"));
		oneHolidayCalendar.setDate(rs.getDate("date"));
		oneHolidayCalendar.setDescription(rs.getString("description"));
	}

} catch (Exception e) {
	String error = "Error selecting holiday calendar. Nested Exception is: "
			+ e;
	Logger.getLogger(getClass().getName()).log(Level.SEVERE, error);
	throw new DAOSystemException(error);
} finally {
	try {
		mysqlImpl.close();
	} catch (Exception e) {
	}
}

return oneHolidayCalendar;

	}

	@Override
	public ArrayList<HolidayCalendarDTO> findAllHolidayCalendars()
			throws DAOSystemException {
		// TODO Auto-generated method stub
		ArrayList<HolidayCalendarDTO> items = new ArrayList<HolidayCalendarDTO>();
		String selectSql = "SELECT * FROM holidaycalendar;";

		try {

			Logger.getLogger(getClass().getName()).log(Level.INFO,
					"Executing select: " + selectSql);
			ResultSet rs = mysqlImpl.get(selectSql);
			HolidayCalendarDTO oneHolidayCalendar = null;
			String leaveTypeId = null;
			while (rs.next()) {
				oneHolidayCalendar = new HolidayCalendarDTO();
				oneHolidayCalendar.setId(rs.getString("id"));
				oneHolidayCalendar.setDate(rs.getDate("date"));
				oneHolidayCalendar.setDescription(rs.getString("description"));

				items.add(oneHolidayCalendar);
			}

		} catch (Exception e) {
			String error = "Error selecting holiday calendar. Nested Exception is: "
					+ e;
			Logger.getLogger(getClass().getName()).log(Level.SEVERE, error);
			throw new DAOSystemException(error);
		} finally {
			try {
				mysqlImpl.close();
			} catch (Exception e) {
			}
		}
		return items;
	}

	@Override
	public void insertHolidayCalendar(HolidayCalendarDTO holidayCalendar)
			throws DAOSystemException {
		// TODO Auto-generated method stub
		String insertSql = "INSERT INTO holidaycalendar VALUES('" + holidayCalendar.getId()
		+ "', '" + holidayCalendar.getDate() +"', '" + holidayCalendar.getDescription()+"');";

try {

	Logger.getLogger(getClass().getName()).log(Level.INFO,
			"Executing insert: " + insertSql);
	int res = mysqlImpl.execute(insertSql);

} catch (Exception e) {
	String error = "Error selecting holiday calendar. Nested Exception is: "
			+ e;
	Logger.getLogger(getClass().getName()).log(Level.SEVERE, error);
	throw new DAOSystemException(error);
} finally {
	try {
		mysqlImpl.close();
	} catch (Exception e) {
	}
}

		
	}

	@Override
	public void updateHolidayCalendar(HolidayCalendarDTO holidayCalendar)
			throws DAOSystemException {
		// TODO Auto-generated method stub
String updateSql = "UPDATE holidaycalendar SET date = '" + holidayCalendar.getDate()
		+ "', description = '" + holidayCalendar.getDescription()
		+ "' WHERE id='"
		+ holidayCalendar.getId() + "';";

		try {

			Logger.getLogger(getClass().getName()).log(Level.INFO,
					"Executing update: " + updateSql);
			int res = mysqlImpl.execute(updateSql);

		} catch (Exception e) {
			String error = "Error selecting holiday calendar. Nested Exception is: " + e;
			Logger.getLogger(getClass().getName()).log(Level.SEVERE, error);
			throw new DAOSystemException(error);
		} finally {
			try {
				mysqlImpl.close();
			} catch (Exception e) {
			}
		}
		
	}

	@Override
	public void deleteHolidayCalendar(HolidayCalendarDTO holidayCalendar)
			throws DAOSystemException {
		// TODO Auto-generated method stub
		String deleteSql = "DELETE FROM holidaycalendar WHERE id='"
			+ holidayCalendar.getId().trim() + "';";

	try {
		mysqlImpl.execute(deleteSql);
	} catch (SQLException e) {
		String error = "Error deleting holiday calendar. Nested Exception is: " + e;
		Logger.getLogger(getClass().getName()).log(Level.SEVERE, error);
		try {
			mysqlImpl.rollBack();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		throw new DAOSystemException(error);
	} finally {
		try {
			mysqlImpl.close();
		} catch (Exception e) {
		}
	}

}
		
	}


